package br.com.util;


public class Utility {
	
	public static String onlyNumber(String value){
		if(value==null)return "";
		value=value.trim();
		String onlynumber="";
		for(short i=0;i<value.length();++i){
			if(value.charAt(i)-0x30>=0x00 && value.charAt(i)-0x30<=0x09){
				onlynumber+=value.charAt(i);
			}
		}
		return onlynumber;
	}
	
	public static boolean checkOnlyNumber(String number){
		if(number==null||(number=number.trim()).length()<=0)return false;
		for(short i=0;i<number.length();++i){
			if(number.charAt(i)-0x30<0x00||number.charAt(i)-0x30>0x09)
				return false;
		}
		return true;
	}
	
	public static boolean ValidaCNPJ(String cnpj){
		String num=onlyNumber(cnpj);
		if(num.length()!=14)return false;
		char[] valores=num.toCharArray();
		short dv=0;
		for(short i=11,coef=2;i>=0;--i){
			if(coef>9)coef=2;
			dv+=(valores[i]-0x30)*coef++;
		}
		dv=(short)((dv%11>1)?11-(dv%11):0);
		if(valores[12]-0x30!=dv)return false;
		dv=0;
		for(short i=12,coef=2;i>=0;--i){
			if(coef>9)coef=2;
			dv+=(valores[i]-0x30)*coef++;
		}
		dv=(short)((dv%11>1)?11-(dv%11):0);
		if(valores[13]-0x30!=dv)return false;
		return true;
	}
	
	public static String tagErrorGen(String nome){
		return "campo "+nome+" inválido";
	}

}
